package com.ma4567.remotecontroller.InfoRequest;

import cn.hutool.system.SystemUtil;
import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.TableLogManager;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import oshi.SystemInfo;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;

class getSystemUsageResponse extends com.ma4567.remotecontroller.util.CustomRespone {
    public double Cpu = 0;
    public double Mem = 0;
    public ArrayList<Double> Disk = new ArrayList<>();

    public getSystemUsageResponse(int Status) {
        super(Status);
        if(Status == 0){
            SystemInfo systemInfo = new SystemInfo();
            HardwareAbstractionLayer hardware = systemInfo.getHardware();

            CpuInfo cpuInfo = OshiUtil.getCpuInfo();
            double free = cpuInfo.getFree();
            DecimalFormat format = new DecimalFormat("#.00");
            this.Cpu = Double.parseDouble(format.format(100.0D - free));

            //this.Mem = (double) (OshiUtil.getMemory().getAvailable() / OshiUtil.getMemory().getTotal()) /1024/1024;
            double usedMem = (double) (OshiUtil.getMemory().getAvailable() / 1024 / 1024 / 1024);
            double TotalMem = (double) (OshiUtil.getMemory().getTotal() / 1024 / 1024 / 1024) + 1;

            String str1 = String.format("%.2f", 100 - (usedMem / TotalMem * 100));
            this.Mem = (Double.parseDouble(str1));

            FileSystem fileSystem = systemInfo.getOperatingSystem().getFileSystem();
            for(OSFileStore fs: fileSystem.getFileStores()){
                double totalSpace = (double)(fs.getTotalSpace() / 1024 / 1024 / 1024);
                double freeSpace = (double)(fs.getUsableSpace() / 1024 / 1024 / 1024);

                String str = String.format("%.2f", (totalSpace - freeSpace) / totalSpace * 100);

                this.Disk.add(Double.parseDouble(str));
            }

        }
    }
}

@CrossOrigin
@RestController
public class GetSystemUsage {
    @RequestMapping(value = "/api/getSystemUsage", method = RequestMethod.GET)
    public String getSystemUsage(HttpServletRequest request,
                                 @RequestParam("cookie") String Cookie) throws SQLException {
        ffmpegController.StopStream();
        TableRequestLog.AddLog("Request system usage", Cookie, request.getRemoteAddr());
        if(!RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴权
            return JSONObject.toJSONString(new getSystemUsageResponse(1), SerializerFeature.PrettyFormat);
        }
        else {
            TableLogManager.AddLog("Request system usage", Cookie);
            return JSONObject.toJSONString(new getSystemUsageResponse(0), SerializerFeature.PrettyFormat);
        }
    }
}
